package vn.bk.hotel.adapter;

import java.util.ArrayList;

import vn.bk.hotel.object.HotelInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteAdapter extends SQLiteOpenHelper {

    public static final String KEY_ID = "id";
    public static final String KEY_NAME = "name";
    public static final String KEY_ADDRESS = "address";
    public static final String KEY_TYPE = "type";
    public static final String KEY_RATING = "rating";
    public static final String KEY_URL_BITMAP = "url";
    public static final String KEY_LONG = "longitude";
    public static final String KEY_LAT = "latitude";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_URI = "uri";
    public static final String KEY_DESCRIPTION = "description";

    public static final String DATABASE_NAME = "hotel.sqlite";
    public static final String DATABASE_TABLE = "favoritehotel";

    public static final String[] allColumns = { KEY_ID, KEY_NAME, KEY_TYPE,
	    KEY_ADDRESS, KEY_RATING, KEY_URL_BITMAP, KEY_LONG, KEY_LAT,
	    KEY_PHONE, KEY_URI, KEY_DESCRIPTION };

    private SQLiteDatabase mDB;

    private static final String DATABASE_CREATE = "create table "
	    + DATABASE_NAME + " (" + KEY_ID
	    + " integer primary key autoincrement, " + KEY_NAME
	    + " text not null, " + KEY_TYPE + " text not null, " + KEY_ADDRESS
	    + " text not null, " + KEY_RATING + " integer not null, "
	    + KEY_URL_BITMAP + " text, " + KEY_LONG + " double, " + KEY_LAT
	    + " double, " + KEY_PHONE + " text, " + KEY_URI + " text, "
	    + KEY_DESCRIPTION + " text);";

    public static final int DATABASE_VERSION = 2;

    public SQLiteAdapter(Context context, String name, CursorFactory factory,
	    int version) {
	super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
	// TODO Auto-generated method stub
	db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	// TODO Auto-generated method stub

    }

    public SQLiteAdapter open() {
	mDB = getWritableDatabase();
	return this;
    }

    public void close() {
	close();
    }

    public void addHotel(HotelInfo myHotel) {
	ContentValues values = new ContentValues();
	values.put(KEY_NAME, myHotel.getNameHotel());
	values.put(KEY_TYPE, myHotel.getType());
	values.put(KEY_ADDRESS, myHotel.getAdress());
	values.put(KEY_RATING, myHotel.getNumRate());
	values.put(KEY_URL_BITMAP, myHotel.getUrlBitMapHotel());
	values.put(KEY_LONG, myHotel.getLongitude());
	values.put(KEY_LAT, myHotel.getLatitude());
	values.put(KEY_PHONE, myHotel.getPhone());
	values.put(KEY_URI, myHotel.getUri());
	values.put(KEY_DESCRIPTION, myHotel.getDescription());

	long insertId = mDB.insert(DATABASE_TABLE, null, values);

	Cursor cursor = mDB.query(DATABASE_TABLE, allColumns, KEY_ID + " = "
		+ insertId, null, null, null, null);
	cursor.moveToFirst();
	cursor.close();
    }

    public void deleteHotel(HotelInfo myHotel) {
	mDB.delete(DATABASE_TABLE, KEY_NAME + " = " + myHotel.getNameHotel(),
		null);
    }

    public boolean isExistInDatabase(HotelInfo myHotel) {
	ArrayList<HotelInfo> allHotel = getAllHotel();
	for (int i = 0; i < allHotel.size(); i++) {
	    if (allHotel
		    .get(i)
		    .getNameHotel()
		    .equals(myHotel.getNameHotel().equals(myHotel.getNameHotel())
			    && allHotel.get(i).getUri()
				    .equals(myHotel.getUri()))) {
		return true;
	    }
	}
	return false;
    }

    public ArrayList<HotelInfo> getAllHotel() {
	open();
	ArrayList<HotelInfo> listHotel = new ArrayList<HotelInfo>();
	Cursor cursor = mDB.query(DATABASE_TABLE, allColumns, null, null, null,
		null, null);
	cursor.moveToFirst();
	while (!cursor.isAfterLast()) {
	    HotelInfo hotel = new HotelInfo(cursor.getString(1),
		    cursor.getString(2), cursor.getString(3), cursor.getInt(4),
		    cursor.getString(5), cursor.getDouble(6),
		    cursor.getDouble(7), cursor.getString(8),
		    cursor.getString(9), cursor.getString(10), false);
	    listHotel.add(hotel);
	    cursor.moveToNext();
	}
	cursor.close();
	return listHotel;
    }

}
